package im.yisme.model;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import im.yisme.model.fileio.MusicCopy;
import im.yisme.model.fileio.MusicRepositoryReader;
import im.yisme.model.fileio.MusicRepositoryWriter;
import im.yisme.model.service.MusicRepositoryService;

import im.yisme.vo.MusicVO;

public class MusicRepository implements MusicRepositoryService{

	private static MusicRepository musicRepository;
	private ArrayList<Music> list;
	
	private MusicRepository(){
		
	}
	
	public static MusicRepository getInstance(){
		return musicRepository == null ? musicRepository = new MusicRepository() : musicRepository;
	}
	
	public Iterator<MusicVO> getMusicList(){
		if (list == null)
			return null;
		
		ArrayList<MusicVO> tempList = new ArrayList<MusicVO>();
		for (Music music: list){
			tempList.add(music.toVO());
		}
		return tempList.iterator();
	}
	
	public ArrayList<Music> getRealList(){
		return list;
	}
	
	public void setMusicList(ArrayList<Music> list){
		this.list = list;
	}
	
	public void loadFile(){
		MusicRepositoryReader reader = new MusicRepositoryReader();
		try {
			reader.read(this);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public MusicVO findMusic(long musicId){
		MusicVO result = null;
		
		for (Music music : list){
			if (music.getId() == musicId){
				result = music.toVO();
				break;
			}
		}
		
		return result;
	}
	
	private boolean isFileValid(String filePath){
		if (filePath.startsWith("http")) return true;
		
		File file = new File(filePath);
		if (!file.exists())
			return false;
		
		String temp = file.getName();
		String name = temp.toLowerCase();
		if (!name.endsWith(".mp3"))
			return false;
		
		return true;
	}

	public long addMusicToRepository(MusicVO toAdd) {
		// TODO Auto-generated method stub
		try {
			String filePath = toAdd.getFilePath();
			if (!isFileValid(filePath))
				return -1;
			
			long id = getNewId();
			MusicCopy musicCopy = new MusicCopy();
			String realPathString = musicCopy.copy(filePath, id);
			
			Music music = new Music(id, toAdd.getArtist(), toAdd.getName(), toAdd.getDuration());
			music.setFilePath(realPathString);
			list.add(music);
			updateLocalData();
			return music.getId();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return -1;
		}
	}

	public boolean removeMusicFromRepository(MusicVO toRemove) {
		// TODO Auto-generated method stub
		try {
			for (Music music : list){
				if (music.getId() == toRemove.getId()){
					list.remove(music);
					break;
				}
			}
			updateLocalData();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean alterMusicInfo(MusicVO musicVO) {
		// TODO Auto-generated method stub
		try {
			for (Music music : list){
				if (music.getId() == musicVO.getId()){
					music.setArtist(musicVO.getArtist());
					music.setName(musicVO.getName());
					break;
				}
			}
			updateLocalData();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	private void updateLocalData() throws IOException{
		new MusicRepositoryWriter().write(this);
	}
	
	private long getNewId(){
		return list.size() == 0 ? 1 : list.get(list.size()-1).getId() + 1;
	}
}
